<template>
  <div>
    <div class="box">
      <div class="head">
        <div class="flex">
          <el-button @click="back" icon="el-icon-arrow-left">返回</el-button>
          <span style="margin-left: 10px">文章详情</span>
        </div>
      </div>
      
      <div v-if="articleInfo.id" class="article-container">
        <!-- 文章头部信息 -->
        <div class="article-header">
          <h1 class="article-title">{{ articleInfo.title }}</h1>
          <div class="article-meta">
            <span class="meta-item">
              <i class="el-icon-folder"></i>
              分类：{{ articleInfo.type_name }}
            </span>
            <span class="meta-item">
              <i class="el-icon-view"></i>
              阅读量：{{ articleInfo.see_total }}
            </span>
            <span class="meta-item">
              <i class="el-icon-time"></i>
              发布时间：{{ articleInfo.create_time }}
            </span>
            <span class="meta-item">
              <i class="el-icon-user"></i>
              来源：{{ articleInfo.from_type_text }}
            </span>
            <el-tag v-if="articleInfo.is_top" type="success" class="meta-item">
              <i class="el-icon-top"></i>
              置顶
            </el-tag>
          </div>
        </div>

        <!-- 文章封面图片 -->
        <div v-if="articleInfo.img_url" class="article-cover">
          <el-image
            :src="articleInfo.img_url"
            :preview-src-list="[articleInfo.img_url]"
            alt="文章封面"
            class="cover-image"
          ></el-image>
        </div>

        <!-- 文章描述 -->
        <div v-if="articleInfo.desc" class="article-desc">
          <h3>文章摘要</h3>
          <p>{{ articleInfo.desc }}</p>
        </div>

        <!-- 文章内容 -->
        <div class="article-content">
          <h3>文章内容</h3>
          <div class="content-body" v-html="articleInfo.content"></div>
        </div>
      </div>

      <div v-else class="loading-container">
        <el-empty description="文章不存在或加载失败"></el-empty>
      </div>
    </div>
  </div>
</template>

<script>
import { ArticleDetail } from '@/api/manage';

export default {
  name: 'articleDetail',
  data() {
    return {
      articleInfo: {},
      loading: false,
    };
  },
  mounted() {
    this.getArticleDetail();
  },
  methods: {
    getArticleDetail() {
      const id = this.$route.query.id;
      if (!id) {
        this.$message.error('文章ID不能为空');
        return;
      }
      
      this.loading = true;
      ArticleDetail({ id }).then((res) => {
        this.articleInfo = res.data.info;
        this.loading = false;
      }).catch((error) => {
        this.$message.error('获取文章详情失败');
        this.loading = false;
      });
    },
    back() {
      this.$router.back();
    },
  },
};
</script>

<style scoped>
.head {
  width: 100%;
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}
.flex {
  display: flex;
  align-items: center;
}
.box {
  width: 100%;
  background: #ffffff;
  border-radius: 5px;
  padding: 20px;
}
.article-container {
  max-width: 800px;
  margin: 0 auto;
}
.article-header {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #e4e7ed;
}
.article-title {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin: 0 0 15px 0;
  line-height: 1.4;
}
.article-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  color: #909399;
  font-size: 14px;
}
.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
}
.meta-item i {
  font-size: 14px;
}
.article-cover {
  margin-bottom: 30px;
  text-align: center;
}
.cover-image {
  max-width: 100%;
  max-height: 400px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.article-desc {
  margin-bottom: 30px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
  border-left: 4px solid #409eff;
}
.article-desc h3 {
  margin: 0 0 10px 0;
  color: #303133;
  font-size: 16px;
}
.article-desc p {
  margin: 0;
  color: #606266;
  line-height: 1.6;
}
.article-content {
  margin-bottom: 30px;
}
.article-content h3 {
  margin: 0 0 20px 0;
  color: #303133;
  font-size: 18px;
  padding-bottom: 10px;
  border-bottom: 2px solid #409eff;
}
.content-body {
  color: #606266;
  line-height: 1.8;
  font-size: 16px;
}
.content-body >>> p {
  margin-bottom: 16px;
}
.content-body >>> img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  margin: 10px 0;
}
.content-body >>> h1,
.content-body >>> h2,
.content-body >>> h3,
.content-body >>> h4,
.content-body >>> h5,
.content-body >>> h6 {
  margin: 20px 0 10px 0;
  color: #303133;
}
.loading-container {
  text-align: center;
  padding: 50px 0;
}
</style> 